<template>
  <div class="my-container">
    <div v-if="!user.token" class="noLogin header">
      <img
        class="mobile-img"
        src="~@/assets/mobile.png"
        @click="$router.push('/login')"
      >
    </div>

    <div v-else class="userInfo header">
      <div class="base">
        <div class="left">
          <van-image
            fit="cover"
            :src="userInfo.photo"
            class="avatar"
            round
          />
          <span class="name">{{ userInfo.name }}</span>
        </div>
        <div class="right">
          <van-button type="default" size="mini" round to="/user/profile">编辑资料</van-button>
        </div>
      </div>

      <div class="data">
        <div class="data-item">
          <span>{{ userInfo.art_count }}</span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span>{{ userInfo.follow_count }}</span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span>{{ userInfo.fans_count }}</span>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <span>{{ userInfo.like_count }}</span>
          <span>获赞</span>
        </div>
      </div>
    </div>

    <div class="nav-grid">
      <van-grid :column-num="2" clickable>
        <van-grid-item>
          <template #icon>
            <i class="toutiao toutiao-shoucang"></i>
          </template>
          <template #text>
            <span class="text" slot="text">收藏</span>
          </template>
        </van-grid-item>
        <van-grid-item>
          <template #icon>
            <i class="toutiao toutiao-lishi"></i>
          </template>
          <template #text>
            <span class="text" slot="text">历史</span>
          </template>
        </van-grid-item>
      </van-grid>
    </div>

    <van-cell title="消息通知" is-link url="" />
    <van-cell title="实名认证" is-link url="" />

    <van-cell
      @click="onLogout"
      v-if="user.token"
      class="logout-cell"
      title="退出登录"
      center
    />
  </div>
</template>

<script>
import { mapState } from 'vuex'
// 3. 导入 getuser请求方法
import { getUser } from '@/api/user'

export default {
  data() {
    return {
      userInfo: {}
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created() {
    // **! 当 登陆了再发请求 **
    if(!this.user.token) {
      return false
    }
    // 4. 页面已加载 我们就调用方法发起请求
    this.getUserInfo()
  },
  methods: {
    onLogout() {
      this.$dialog.confirm({
        title: '退出',
        message: '确认退出吗'
      }).then(() => {
        this.$store.commit('setToken', {})

        this.$toast('已成功退出')
      }).catch(() => {
        this.$toast('取消退出')
      })
    },
    async getUserInfo() {
      // 5. 发起请求
      const {data:{data}} = await getUser()

      // 6. 将请求下来的数据赋值
      this.userInfo = data
    }
  }
}
</script>

<style scoped lang="less">
.my-container {
  .noLogin {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 361px;
    background: url("~@/assets/banner.png") no-repeat;
    background-size: cover;
    .mobile-img {
      width: 132px;
      height: 132px;
    }
  }

  .userInfo {
    background: url("~@/assets/banner.png") no-repeat;
    background-size: cover;
    .base {
      height: 231px;
      padding: 76px 32px 23px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      box-sizing: border-box;
      .left {
        display: flex;
        align-items: center;
        .avatar {
          width: 132px;
          height: 132px;
          border: 2px solid #fff;
        }
        .name {
          color: #fff;
          font-size: 29px;
          margin-left: 22px;
        }
      }
    }
    .data{
      display: flex;
      justify-content: space-around;
      .data-item {
        height: 130px;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        color: #fff;
        span:nth-child(1) {
          font-size: 36px;
          margin-bottom: 15px;
        }
        span:nth-child(2) {
          font-size: 23px;
        }
      }
    }
  }

  .nav-grid {
    background: #fff;
    .toutiao {
      font-size: 45px;
    }
    .toutiao-shoucang {
      color: #EB5253;
    }
    .toutiao-lishi {
      color: #FF9D1D;
    }
    .text {
      color: #000;
      margin-top: 8px;
      font-size: 28px;
    }
  }

  .logout-cell {
    margin-top: 10px;
    text-align: center;
    color: red;
    border: 1px solid red;
  }
}
</style>
